package org.tong.account.service;

import io.seata.spring.annotation.GlobalTransactional;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tong.account.jpa.AccountJpaRepository;
import org.tong.account.jpa.beans.Account;

import java.util.Random;

/**
 * @author
 * @date 2022-12-21 16:31
 */
@Service
public class AccountService {

    @Autowired
    private AccountJpaRepository accountJpaRepository;

    @GlobalTransactional
    public void addAccount(Long id, String name) {
        Account account = new Account();
        account.setId(id);
        account.setName(name);
        account.setCode("test-" + id);
        account.setAmount(0L);
        account.setCreateTime(System.currentTimeMillis());
        int randomInt = new Random().nextInt(10);
        if (randomInt >= 5) {
            throw new RuntimeException("随机错误");
        }
        accountJpaRepository.save(account);
    }
}
